package com.qifan.energy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qifan.energy.R;
import com.qifan.energy.adapter.CouponListAdapter;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.result.CouponItem;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.dialog.LoadingUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MessageActivity extends BaseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    CouponListAdapter adapter;
    private List dataList = new ArrayList<CouponItem>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        adapter = new CouponListAdapter(dataList);
        /* 监听子控件点击 */
        adapter.setOnItemChildClickListener(this::onItemChildClick);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        //添加空布局
        adapter.setEmptyView(getEmptyDataView());
    }

    /* 获取空布局 */
    private View getEmptyDataView() {
        return getLayoutInflater().inflate(R.layout.empty_list_quick, recyclerView, false);
    }

    @Override
    protected void initData() {
        memberCouponList();
    }

    //点击立即使用
    private void onItemChildClick(BaseQuickAdapter a, View v, int p) {
        switch (v.getId()) {
            case R.id.use_now_tv:
                ToastUtils.showShort("item");
                break;
            default:
                break;
        }
    }

    /* 获取用户优惠券列表 0->未使用 */
    private void memberCouponList(){
        LoadingUtils.show(this);
        NetUtils.memberCouponList(0,new MyObserver<List<CouponItem>>(this) {
            @Override
            public void onSuccess(List<CouponItem> result) {
                LoadingUtils.hide();
                if (null != result){
                    dataList = result;
                    adapter.setNewData(result);
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);
            }
        });
    }
}
